package cn.tedu.tmall.front.mall.service.impl;

import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.pojo.po.DistrictSimplePO;
import cn.tedu.tmall.front.mall.dao.persist.cache.IDistrictCacheRepository;
import cn.tedu.tmall.front.mall.dao.persist.repository.IReceiverAddressRepository;
import cn.tedu.tmall.front.mall.pojo.entity.ReceiverAddress;
import cn.tedu.tmall.front.mall.pojo.param.ReceiverAddressAddNewParam;
import cn.tedu.tmall.front.mall.pojo.param.ReceiverAddressUpdateParam;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressStandardVO;
import cn.tedu.tmall.front.mall.service.IReceiverAddressService;
import io.jsonwebtoken.lang.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;

@Service
public class ReceiverAddressServiceImpl implements IReceiverAddressService {
    @Autowired
    private IReceiverAddressRepository repository;
    @Autowired
    private IDistrictCacheRepository cacheRepository;
    @Override
    public void addNew(CurrentPrincipal principal, ReceiverAddressAddNewParam addNewParam) {
        DistrictSimplePO province = cacheRepository.getByCode(addNewParam.getProvinceCode());
        DistrictSimplePO city = cacheRepository.getByCode(addNewParam.getCityCode());
        if(province==null||city==null){
            String message = "添加收货地址失败，选择的地址数据有误";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        ReceiverAddress address = new ReceiverAddress();
        BeanUtils.copyProperties(addNewParam,address);
        address.setUserId(principal.getId());
        address.setProvince(province.getName());
        address.setCity(city.getName());
        if(StringUtils.hasText(addNewParam.getAreaCode())){
            DistrictSimplePO area = cacheRepository.getByCode(addNewParam.getAreaCode());
            address.setArea(area.getName());
        }
        if(addNewParam.getIsDefault()==1){
            repository.updateNonDefaultByUserId(principal.getId());
        }

        int rows = repository.insert(address);
        if(rows!=1){
            String message = "添加收货地址失败，服务器繁忙，请稍后重试";
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }

    }

    @Override
    public void deleteById(Long id) {
        ReceiverAddressStandardVO addressStandardVO = repository.selectById(id);
        if (addressStandardVO == null) {
            String message = "删除地址失败，该收获地址不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        int rows = repository.deleteById(id);
        if(rows!=1){
            String message = "删除收货地址失败，服务器繁忙，请稍后重试";
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
    }

    @Override
    public void updateById(CurrentPrincipal principal, Long id, ReceiverAddressUpdateParam updateParam) {
        DistrictSimplePO province = cacheRepository.getByCode(updateParam.getProvinceCode());
        DistrictSimplePO city = cacheRepository.getByCode(updateParam.getCityCode());
        if (province == null || city == null) {
            String message = "修改收货地址失败，选择的地区数据有误！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        ReceiverAddressStandardVO queryResult = repository.selectById(id);
        if (queryResult == null) {
            String message = "修改收货地址失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        if (!queryResult.getUserId().equals(principal.getId())) {
            String message = "修改收货地址失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        ReceiverAddress receiverAddress = new ReceiverAddress();
        BeanUtils.copyProperties(updateParam, receiverAddress);
        receiverAddress.setId(id);
        receiverAddress.setProvince(province.getName());
        receiverAddress.setCity(city.getName());
        if (StringUtils.hasText(updateParam.getAreaCode())) {
            DistrictSimplePO area = cacheRepository.getByCode(updateParam.getAreaCode());
            receiverAddress.setArea(area.getName());
        }

        int rows = repository.updateById(receiverAddress);
        if (rows != 1) {
            String message = "修改收货地址失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public ReceiverAddressStandardVO selectById(CurrentPrincipal principal, Long id) {
        ReceiverAddressStandardVO addressStandardVO = repository.selectById(id);
        if (addressStandardVO == null) {
            String message = "查询地址失败，该收获地址不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if(addressStandardVO.getUserId()!=principal.getId()){
            String message = "查询地址失败，该收获地址不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return addressStandardVO;
    }

    @Override
    public List<ReceiverAddressListItemVO> listByUserId(CurrentPrincipal principal) {
        List<ReceiverAddressListItemVO> addressListItemVOS = repository.listByUserId(principal.getId());
        return addressListItemVOS;
    }
}
